package symboltable;



import syntaxtree.IdentifierType;
import syntaxtree.Type;

import java.util.Hashtable;

import java.util.Vector;

import syntaxtree.Formal;

public class Class {
	
	private Hashtable<Symbol, Type> variables;	// Represents the fields of a Class
	// Represents a set of Methods
	// we can have methods with the same name, thus with the same symbol
	private Vector<Symbol> methods_symbols;
	private Vector<Method> methods;
	
	private Symbol cs;
	private Symbol ex;
	
	private Type type;
	
	// Constructor for a class that extends other
	public Class(Symbol c, Symbol e)
	{
		cs = c;
		ex = e;
		
		type = new IdentifierType(cs.toString());
		
		variables = new Hashtable<Symbol, Type>();
		methods_symbols = new Vector<Symbol>();
		methods = new Vector<Method>();
	}
	
	// Constructor for a class
	public Class(Symbol c)
	{
		cs = c;
		ex = null;
		
		variables = new Hashtable<Symbol, Type>();
		methods_symbols = new Vector<Symbol>();
		methods = new Vector<Method>();
	}
	
	public Symbol get_extended_class()
	{
		return ex;
	}
	
	public Symbol get_class()
	{
		return cs;
	}
	
	public Type get_type()
	{
		return type;
	}
	
	// Returns null in case a method with symbol m doesnt exists
	public Vector<Method> get_methods(Symbol m)
	{
		Vector<Method> meths = new Vector<Method>();
		for(int i = 0; i < methods_symbols.size(); i++)
		{
			if(m.equals(methods_symbols.get(i))) {
				meths.add(methods.get(i));
			}
		}
		return meths;
	}
	
	public boolean contains_variable(Symbol s)
	{
		return variables.containsKey(s);
	}
	
	public Type get_variable_type(Symbol s)
	{
		return variables.get(s);
	}
	
	public void add_variable(Symbol s, Type t)
	{
		variables.put(s, t);
	}
	
	public Method add_method(Symbol s, Vector<Formal> p, Type t)
	{
		Method m = new Method(t,s,p);
		methods.add(m);
		methods_symbols.add(s);
		return m;
	}
	
}
